package com.gitee.feizns.itrip.personal.utils;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author feizns
 * @since 2019/1/16 0016
 */
public class FTPUtils implements AutoCloseable {

    /**
     * 默认编码
     */
    public static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * 默认的缓存大小
     */
    public static final int DEFAULT_BUF_SIZE = 1024;

    private FTPClient ftpClient;

    private String basePath;

    public FTPUtils(String serverIP, int port, String userName, String password, String basePath) throws IOException {
        ftpClient = new FTPClient();
        ftpClient.connect(serverIP, port);
        ftpClient.login(userName, password);
        ftpClient.setBufferSize(DEFAULT_BUF_SIZE);
        ftpClient.setControlEncoding(DEFAULT_CHARSET);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        this.basePath = basePath;
    }

    /**
     * 下载ftp文件到本地
     * @param dir
     * @param fileName
     * @param localFile
     * @return
     * @throws IOException
     */
    public boolean downloadFile(String dir, String fileName, String localFile) {
        try ( OutputStream os = new FileOutputStream(localFile) ) {
            return downloadFile(dir, fileName, os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean downloadFile(String dir, String fileName, OutputStream os) {
        try {
            if ( dir != null )
                changeDirectory(basePath + dir );
            return ftpClient.retrieveFile(fileName, os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件制定目录
     * @param dir
     * @param fileName
     * @param localFile
     * @return
     * @throws IOException
     */
    public boolean uploadFile(String dir, String fileName, String localFile) {
        try ( InputStream is = new FileInputStream(localFile) ) {
            return uploadFile(dir, fileName, is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件到指定目录
     * @param dir
     * @param fileName
     * @param is
     * @return
     */
    public boolean uploadFile(String dir, String fileName, InputStream is) {
        try {
            if ( dir != null )
                changeDirectory(basePath + dir);
            System.out.println(basePath + dir);
            return ftpClient.storeFile(fileName, is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 切换目录
     * @param path 创建目录
     * @return 创建标志
     * @throws IOException 异常
     */
    public boolean changeDirectory(String path) throws IOException {
        return ftpClient.changeWorkingDirectory(path);
    }

    /**
     * 创建目录
     * @param path 创建目录
     * @return 创建标志
     * @throws IOException 异常
     */
    public boolean createDirectory(String path) throws IOException {
        return ftpClient.makeDirectory(path);
    }

    /**
     * 自动关闭资源
     */
    @Override
    public void close() throws Exception {
        if (ftpClient != null && ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }

}